import base64

from django.test import TestCase
import pickle

# Create your tests here.

if __name__ == "__main__":
    cookie_cart = 'gAN9cQAoSwF9cQEoWAUAAABjb3VudHECSwJYCAAAAHNlbGVjdGVkcQOJdUsFfXEEKGgCSwFoA4h1Swl9cQUoaAJLA2gDiXV1Lg=='

    res = cookie_cart.encode()
    print(res)

    res = base64.b64decode(res)
    print(res)

    # pickle.loads(bytes字节流)：将bytes字节流转换为字典|对象
    res = pickle.loads(res)
    print(res)

    # 解析cookie购物车数据:
    # cart_dict = pickle.loads(base64.b64decode(cookie_cart.encode()))
    cart_dict = pickle.loads(base64.b64decode(cookie_cart))
    print(cart_dict)


# if __name__ == "__main__":
#     # pickle.dumps(<dict>|<object>)：将传入字典|对象数据转换为bytes字节流
#     cart_dict = {
#         1: {
#             'count': 2,
#             'selected': False
#         },
#         5: {
#             'count': 1,
#             'selected': True
#         },
#         9: {
#             'count': 3,
#             'selected': False
#         }
#     }
#
#     # res = pickle.dumps(cart_dict)
#     # print(res)
#     #
#     # res = base64.b64encode(res) # bytes
#     # print(res)
#     #
#     # res = res.decode()
#     # print(res)
#
#     # 设置cookie购物车数据
#     cart_data = base64.b64encode(pickle.dumps(cart_dict)).decode()
#     print(cart_data)